#AutoIt3Wrapper_Au3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6 -w 7 -d
#AutoIt3Wrapper_Run_After=md "%scriptdir%\Versions\%fileversion%"
#AutoIt3Wrapper_Run_After=copy "%in%" "%scriptdir%\Versions\%fileversion%\%scriptfile%%fileversion%.au3"
#AutoIt3Wrapper_Run_After=copy "%out%" "%scriptdir%\Versions\%fileversion%\%scriptfile%%fileversion%.exe"
#AutoIt3Wrapper_UseX64=n

Switch @CPUArch
	Case "X86"
		#AutoIt3Wrapper_UseX64=n
	Case "X64"
		#AutoIt3Wrapper_UseX64=y
EndSwitch

;**** Function Name List ****
; _IM_ColorFormatter
; _IM_ColorVerifier
; _IM_ConvertToCSS
; _IM_CopyImagesToTmpDir
; _IM_Dtor
; _IM_GravityVerifier
; _IM_ShutDown
; IM_AdaptiveBlur
; IM_AdaptiveResize
; IM_AdaptiveSharpen
; IM_Alpha
; IM_Annotate
; IM_Append
; IM_AutoGamma
; IM_AutoLevel
; IM_AutoOrient
; IM_BlackThreshold
; IM_BlueShift
; IM_Blur
; IM_Brightness
; IM_Charcoal
; IM_Comment
; IM_Contrast
; IM_Crop
; IM_Decipher
; IM_Despeckle
; IM_Directory
; IM_Edge
; IM_Emboss
; IM_Encipher
; IM_Extension
; IM_FileName
; IM_Filesize
; IM_Flip
; IM_Flop
; IM_Gamma
; IM_Height
; IM_ImageInfo
; IM_Implode
; IM_ListTypes
; IM_Modulate
; IM_Monochrome
; IM_MyErrFunc
; IM_Name
; IM_Negate
; IM_Normalize
; IM_PageGeometry
; IM_Paint
; IM_Polaroid
; IM_Resize
; IM_Rotate
; IM_Sepiatone
; IM_Spread
; IM_StartUp
; IM_Thumbnail
; IM_Width
; ImageMagickObject

;**** Organize Includes ****
#include-once
#include <Array.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <ClipBoard.au3>

#include "AutoItObject.au3"

#Region ;**** Error Handler ****
Global $oMyError

Func IM_MyErrFunc()
	ConsoleWrite(@LF & _
			"===========================================================" & @LF & _
			"! AutoItCOM Test: We intercepted a COM error!"      & @LF & @TAB & _
			"err.description   : "    & $oMyError.description      & @LF & @TAB & _
			"err.windescription: "    & $oMyError.windescription   & @LF & @TAB & _
			"err.number        : 0x"  & Hex($oMyError.number, 8)   & @LF & @TAB & _
			"err.lastdllerror  : "    & $oMyError.lastdllerror     & @LF & @TAB & _
			"err.scriptline    : ("   & $oMyError.scriptline & ')' & @LF & @TAB & _
			"err.source        : "    & $oMyError.source           & @LF & @TAB & _
			"err.helpfile      : "    & $oMyError.helpfile         & @LF & @TAB & _
			"err.helpcontext   : "    & $oMyError.helpcontext      & @LF & _
			"===========================================================" & @LF)
EndFunc
#EndRegion ;**** Error Handler ****

#Region ;**** ImageMagick ****
; = Class =================================================================================
;Name ....: ImageMagickObject
;Syntax ..: $oObject = ImageMagickObject()
;Returns .: An object to be used in your scripts!
Func ImageMagickObject(Const $ImageMagickDll = "ImageMagickObject.dll")
	Local Const $gravity_list[39] = ["None", "Static", "Forget", "NorthWest", "North", _
									 "NorthEast", "West", "Center", "East", "SouthWest", _
									 "South", "SouthEast", "northwest", "north", "northeast", _
									 "west", "center", "east", "southwest", "south", _
									 "southeast", "NW", "N", "NE", "W", "C", "E", "SW", _
									 "S", "SE", "nw", "n", "ne", "w", "c", "e", "sw", _
									 "s", "se"]

	Local Const $alignment_list[9] = ["Horizontal", "Vertical", "horizontal", "vertical", "+append", "-append", "h", "v", '']

	Local Const $tmp_dir = @ScriptDir & "\tmp\"

	Local $this = _AutoItObject_Class()

	$this.AddProperty("DLL"          , $ELSCOPE_PRIVATE, FileGetShortName($ImageMagickDLL))
	$this.AddProperty("IMObj"        , $ELSCOPE_PRIVATE, '')
	$this.AddProperty("GravityList"  , $ELSCOPE_PRIVATE, $gravity_list)
	$this.AddProperty("AlignmentList", $ELSCOPE_PRIVATE, $alignment_list)
	$this.AddProperty("IM_ERROR"     , $ELSCOPE_PRIVATE, '')
	$this.AddProperty("TmpDir"       , $ELSCOPE_PRIVATE, $tmp_dir)
	$this.AddProperty("Seperator"    , $ELSCOPE_PRIVATE, ',')
	$this.AddProperty("Success"      , $ELSCOPE_PRIVATE, 1)

	$this.AddMethod("AdaptiveBlur"   , "IM_AdaptiveBlur")
	$this.AddMethod("AdaptiveResize" , "IM_AdaptiveResize")
	$this.AddMethod("AdaptiveSharpen", "IM_AdaptiveSharpen")
	$this.AddMethod("Alpha"          , "IM_Alpha")
	$this.AddMethod("Annotate"       , "IM_Annotate")
	$this.AddMethod("Append"         , "IM_Append")
	$this.AddMethod("AutoGamma"      , "IM_AutoGamma")
	$this.AddMethod("AutoLevel"      , "IM_AutoLevel")
	$this.AddMethod("AutoOrient"     , "IM_AutoOrient")
	$this.AddMethod("BlackThreshold" , "IM_BlackThreshold")
	$this.AddMethod("BlueShift"      , "IM_BlueShift")
	$this.AddMethod("Blur"           , "IM_Blur")
	$this.AddMethod("Brightness"     , "IM_Brightness")
	$this.AddMethod("Charcoal"       , "IM_Charcoal")
	$this.AddMethod("Contrast"       , "IM_Contrast")
	$this.AddMethod("Comment"        , "IM_Comment")
	$this.AddMethod("Crop"           , "IM_Crop")
	$this.AddMethod("Decipher"       , "IM_Decipher")
	$this.AddMethod("Despeckle"      , "IM_Despeckle")
	$this.AddMethod("Directory"      , "IM_Directory")
	$this.AddMethod("Edge"           , "IM_Edge")
	$this.AddMethod("Emboss"         , "IM_Emboss")
	$this.AddMethod("Encipher"       , "IM_Encipher")
	$this.AddMethod("Extension"      , "IM_Extension")
	$this.AddMethod("FileName"       , "IM_FileName")
	$this.AddMethod("Filesize"       , "IM_Filesize")
	$this.AddMethod("Flip"           , "IM_Flip")
	$this.AddMethod("Flop"           , "IM_Flop")
	$this.AddMethod("Gamma"          , "IM_Gamma")
	$this.AddMethod("Height"         , "IM_Height")
	$this.AddMethod("ImageInfo"      , "IM_ImageInfo")
	$this.AddMethod("Implode"        , "IM_Implode")
	$this.AddMethod("List"           , "IM_ListTypes")
	$this.AddMethod("Modulate"       , "IM_Modulate")
	$this.AddMethod("Monochrome"     , "IM_Monochrome")
	$this.AddMethod("Name"           , "IM_Name")
	$this.AddMethod("Negate"         , "IM_Negate")
	$this.AddMethod("Normalize"      , "IM_Normalize")
	$this.AddMethod("PageGeometry"   , "IM_PageGeometry")
	$this.AddMethod("Paint"          , "IM_Paint")
	$this.AddMethod("Polaroid"       , "IM_Polaroid")
	$this.AddMethod("Resize"         , "IM_Resize")
	$this.AddMethod("Rotate"         , "IM_Rotate")
	$this.AddMethod("Sepiatone"      , "IM_Sepiatone")
	$this.AddMethod("Spread"         , "IM_Spread")
	$this.AddMethod("StartUp"        , "IM_StartUp")
	$this.AddMethod("Thumbnail"      , "IM_Thumbnail")
	$this.AddMethod("Width"          , "IM_Width")

	$this.AddMethod("ColorFormatter"    , "_IM_ColorFormatter"    , True)
	$this.AddMethod("ColorVerifier"     , "_IM_ColorVerifier"     , True)
	$this.AddMethod("ConvertToCSS"      , "_IM_ConvertToCSS"      , True)
	$this.AddMethod("CopyImagesToTmpDir", "_IM_CopyImagesToTmpDir", True)
	$this.AddMethod("GravityVerifier"   , "_IM_GravityVerifier"   , True)
	$this.AddMethod("ShutDown"          , "_IM_ShutDown"          , True)

	$this.AddDestructor("_IM_Dtor")

	Return $this.Object
EndFunc   ;==>ImageMagickObject

Func _IM_Dtor($This)
	If FileExists($this.TmpDir) Then DirRemove($this.TmpDir, 1)
	If IsObj($this.IMObj) Then $this.ShutDown()
EndFunc

#Region ;**** Method Template ****
; = Method ================================================================================
; Name .....: IM_
; Syntax ...: $oObject.($image, $dest)
; Param(s) .: $image: path to the image
; 			  $dest : path to save the image to after it has been altered
; Success ..: 1 &
; Fail .....: 0 & @error: 1:
; Desc. ....:
;~ Func IM_($This, Const $image, Const $dest)
;~ EndFunc
#EndRegion ;**** Method Template ****

#Region ;**** Identify Methods ****
; = Method ================================================================================
; Name .....: IM_Alpha
; Syntax ...: $oObject.Alpha($image)
; Param(s) .: $image: path to the image
; Success ..:
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_Alpha(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%a", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Comment
; Syntax ...: $oObject.Comment($image)
; Param(s) .: $image: path to the image
; Success ..: Returns the comment property of $image.  Not sure if this works yet.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Comment(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%c", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Directory
; Syntax ...: $oObject.Directory($image)
; Param(s) .: $image: name of the image
; Success ..: Directory of the supplied image.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Directory(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%d", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Extension
; Syntax ...: $oObject.Extension($image)
; Param(s) .: $image: Directory of the image of which to retrieve the image's file extension.
; Success ..: Returns the image's file extension.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Extension($This, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%e", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_FileName
; Syntax ...: $oObject.FileName($image)
; Param(s) .: $image: The directory of the image of which to retrieve the filename.
; Success ..: Filename of $image. ([filename].[format])
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_FileName(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%f", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Filesize
; Syntax ...: $oObject.Filesize($image)
; Param(s) .: $image: path to the image
; Success ..: Returns the filesize of $image in bytes.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Filesize(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%b", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Height
; Syntax ...: $oObject.Height($image)
; Param(s) .: $image: Directory of the image of which to retrieve the image's height in pixels.
; Success ..: Returns the height in pixels of the provided image.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Height(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%h", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_ImageInfo
; Syntax ...: $oObject.ImageInfo($image [, $verbose])
; Param(s) .: $image  : The directory of the image of which you wish to retrieve detailed info.
;			  $verbose: Set to True to output even more detailed info.
; Success ..: Outputs info to the console.
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....: This method prints myriad details about your image to the console.
; Modified .: Werty -- 11/24/2010 "-:"
Func IM_ImageInfo(Const $this, Const $image, Const $verbose = False)
	Switch FileExists($image)
		Case 1
			Switch $verbose
				Case True
					Return $this.IMObj.Identify("-verbose", "-:", $image)
					;Return $this.IMObj.Convert($image, "-format", "%f,%b,%w,%h", "-:")
				Case False
					Return $this.IMObj.Identify($image, "-:")
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_ListTypes
; Syntax ...: $oObject.List($type)
; Param(s) .: type: IM type to list.
; Success ..: Returns a list of IM supported types.
; Fail .....: 0 & @error: 1: $type is not a string.
Func IM_ListTypes(Const $this, Const $type)
	Switch IsString($type)
		Case 1
			Return $this.IMObj.Identify("-list", $type, "-:")
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Name
; Syntax ...: $oObject.Name($image)
; Param(s) .: $image: The directory of the image of which to retrieve the image's file name.
; Success ..: The file name of the specified image. ([name])
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Name(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%t", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_PageGeometry
; Syntax ...: $oObject.($image)
; Param(s) .: $image: path to the image
; 			  $dest: path to save the image to after it has been altered
; Success ..: Returns the page geometry of $image.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_PageGeometry(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%g", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Width
; Syntax ...: $oObject.Width($image)
; Param(s) .: $image: The directory of the image of which to retrieve the image's pixel width.
; Success ..: The width of the image in pixels.
; Fail .....: 1 & @error: 1: Invalid image path provided.
Func IM_Width(Const $this, Const $image)
	Switch FileExists($image)
		Case 1
			Return $this.IMObj.Identify("-format", "%w", "-:", $image)
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc
#EndRegion ;**** Identify Methods ****

#Region ;**** Convert Methods ****
; = Method ================================================================================
; Name .....: IM_AdaptiveBlur
; Syntax ...: $oObject.AdaptiveBlur($image, $dest, $radius [, $sigma]])
; Param(s) .: $image: path to the image
; 			  $dest : path to the save the image to after it has been altered
;			  $radius:
;  			  $sigma: [optional] Standard deviation: default is 1
; Success ..: 1
; Fail .....: 0 & @error: 1: Invalid image path provided.
;						  2: Invalid $radius param.
;						  3: Invalid $sigma param.
;						  4: $radius is negative.
; Desc. ....: "Adaptively blur pixels, with decreasing effect near edges."
Func IM_AdaptiveBlur($This, Const $image, Const $dest, Const $radius, Const $sigma = 1)
	Switch FileExists($image)
		Case 1
			Switch IsInt($radius)
				Case 1
					If $radius < 1 Then Return SetError(4, 0, 0)
					Switch IsInt($sigma)
						Case 1
							Local Const $amount = $radius & 'x' & $sigma
							$this.IMObj.Convert($image, "-adaptive-blur", $amount, $dest)
							Return $this.Success
						Case 0
							SetError(3, 0, 0)
					EndSwitch
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_AdaptiveResize
; Syntax ...: $oObject.AdaptiveResize($image, $dest, $geometry, $filter)
; Param(s) .: $image  : path to the image
; 			  $dest   : path to the save the image to after it has been altered
;			  $geometry:
; Success ..: 1: "Resize the image using data-dependent triangulation."
; Fail .....: 0 & @error: 1: Invalid image path provided.
;						  2: Invalid $geometry param.
; Desc. ....: See Image Geometry for complete details about the geometry argument.
;			  The -adaptive-resize option defaults to data-dependent triangulation. Use the
;			  -filter to choose a different resampling algorithm. Offsets, if present in the
;			  geometry string, are ignored, and the -gravity option has no effect.
Func IM_AdaptiveResize($This, Const $image, Const $dest, Const $geometry, Const $filter = '')
	#forceref $filter
	Switch FileExists($image)
		Case 1
			Local Const $geometry_tmp = StringStripWS($geometry, 8)
			$this.IMObj.Convert($image, "-adaptive-resize", $geometry_tmp, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_AdaptiveSharpen
; Syntax ...: $oObject.AdaptiveSharpen($image, $dest, $radius [, $sigma]])
; Param(s) .: $image: path to the image
; 			  $dest : path to the save the image to after it has been altered
;			  $radius:
;  			  $sigma: [optional] Standard deviation: default is 1
; Success ..: 1
; Fail .....: 0 & @error: 1: Invalid image path provided.
;						  2: Invalid $radius param.
;						  3: Invalid $sigma param.
; Desc. ....: "Adaptively sharpen pixels, with increasing effect near edges."
Func IM_AdaptiveSharpen($This, Const $image, Const $dest, Const $radius, Const $sigma = 1)
	Switch FileExists($image)
		Case 1
			Switch IsInt($radius)
				Case 1
					Switch IsInt($sigma)
						Case 1
							Local Const $amount = String($radius & 'x' & $sigma)
							$this.IMObj.Convert($image, "-adaptive-sharpen", $amount, $dest)
							Return $this.Success
						Case 0
							Return SetError(3, 0, 0)
					EndSwitch
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Annotate
; Syntax ...: $oObject.Annotate($image, $dest, $degrees, $text)
; Param(s) .: $image : An image to blur.
;			  $dest  : The directory to place the blurred image..
;			  $degrees:
;			  $text  : Text to annotate image.
; Success ..: A new image that has been annotated with the desired text.
; Fail .....: 0 & @error: 1: Invalid image path provided.
;						  2: $degrees is equal to or less than zero.
;						  3: $text is empty.
Func IM_Annotate($This, Const $image, Const $dest, Const $degrees, Const $text)
	Switch FileExists($image)
		Case 1
			Switch $text
				Case ''
					Return SetError(3, 0, 0)
				Case Else
					Select
						Case $degrees > 0
;~ 							Local Const $t_x = 0, $t_y = 0
							$this.IMObj.Convert($image, "-annotate", $degrees, $text, $dest) ; & 'x' & $degrees & $t_x & ' ' & $t_y
							Return $this.Success
						Case Else
							Return SetError(2, 0, 0)
					EndSelect
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name ...: IM_Append
; Syntax .: $oObject.Append($image_list, $dest [, $alignment [, $gravity]])
; Param(s): $image_list: An array, directory, or a comma separated string containing the images to be appended.
;			$dest    : The directory to which to move the appended image.
;			$alignment: Horizontal | Vertical: Alignment of the appended images.
;			$gravity : Indicates how ImageMagick should center images of different sizes. [Default: northwest]
;           $color   : Color of the background. [Default: white]
; Success : A new image comprised of two or more appended images.
; Fail ...: 0 & @error: 1: Temporary directory failed to create.
;						2: The images were not copied to the temporary directory.
; 						3: The images were not apppended.
;						4: Invalid $alignment parameter.
; Desc. ..: 'Append' works by copying the given images over to a temporary, hidden directory.  ImageMagick then
;			 takes every image in the directory and appends them into one image.  That image is then written to
;			 the given destination directory.
Func IM_Append($This, Const $image_list, Const $dest, $alignment = '', $gravity = '', $color = '')
	Switch DirCreate($this.tmpDir) ; IMOObject needs to create a temp directory to move the images into
		Case 1
			Switch $this.CopyImagesToTmpDir($image_list)
				Case 0
					$gravity = $this.GravityVerifier($gravity)
					$color = $this.ColorFormatter($color)
					$alignment = StringLower($alignment)

					Switch $alignment
						Case '', ' ', "horizontal", 'h', '+', "+append"
							$alignment = "+append"
						Case "vertical", 'v', '-', "-append"
							$alignment = "-append"
						Case Else
							Return SetError(4, 0, 0)
					EndSwitch

					Local Const $check_err = $this.IMObj.Convert($this.tmpDir & "*.*", "-background", $color, "-gravity", $gravity, $alignment, $dest)

					If $check_err = $this.IM_ERROR Then
						Return SetError(3, 0, 0)
					Else
						Return $this.Success
					EndIf
				Case 1
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_AutoGamma
; Syntax ...: $oObject.AutoGamma($image, $dest)
; Param(s) .: $image: path to the image of which to change gamma
;			  $dest: path to the save the image to after it has been altered
; Success ..: An image whose gamma has been adjusted.
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_AutoGamma($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-auto-gamma", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_AutoLevel
; Syntax ...: $oObject.AutoLevel($image, $dest)
; Param(s) .: $image: path to the image of which to change the level
;			  $dest: path to the save the image to after it has been altered
; Success ..: An image whose level has been adjusted.
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_AutoLevel($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-auto-level", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_AutoOrient
; Syntax ...: $oObject.AutoOrient($image, $dest)
; Param(s) .: $image: path to the image of which to change the orientation
;			  $dest: path to the save the image to after it has been altered
; Success ..: An image whose orientation has been adjusted.
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_AutoOrient($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-auto-orient", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_BlackThreshold
; Syntax ...: $oObject.BlackThreshold($image, $dest)
; Param(s) .: $image: path to the image
; 			  $dest: path to the save the image to after it has been altered
; Success ..:
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_BlackThreshold($This, Const $image, Const $dest, Const $threshold)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-black-threshold", $threshold, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_BlueShift
; Syntax ...: $oObject.BlueShift($image, $dest, $factor)
; Param(s) .: $image: The directory of the image of which you wish to alter the blueshift.
;			  $dest : Final directory of the altered image.
;			  $factor: Amount by which to increase or decrease the blueshift.
; Success ..: A new image that has been blueshifted.
; Fail .....: 0 & @error: 1: Invalid image path provided.
;                         2: Factor is not a number.
; Desc. ....: Makes your image look like it was taken at night time.  Start with a factor 1.5
;			  according to ImageMagick.com
Func IM_BlueShift($This, Const $image, Const $dest, Const $factor)
	Local $_factor = $factor

	Switch FileExists($image)
		Case 1
			Switch IsNumber($_factor)
				Case 1
					If $_factor < 1.5 And $_factor >= 0 Then
						$_factor = 1.5
					EndIf

					$this.IMObj.Convert($image, "-blue-shift", $_factor, $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Blur
; Syntax ...: $oObject.Blur($image, $dest, $radius)
; Param(s) .: $image: An image to blur.
;			  $dest: The directory to place the blurred image..
;			  $sigma
;			  $radius
; Success ..: A new image that has been blurred.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Blur($This, Const $image, Const $dest, Const $sigma, Const $radius = 0)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-blur", $radius, $sigma, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Brightness
; Syntax ...: $oObject.Brightness($image, $dest, $brightness)
; Param(s) .: $image 	 : The directory of the image of which you wish to alter the brightness.
;			  $dest      : Final directory of the altered image.
;			  $brightness: Amount by which to increase or decrease the brightness.
; Success ..: A new image that has either been brightened or darkened.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Brightness($This, Const $image, Const $dest, Const $brightness)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-brightness-contrast", $brightness, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Charcoal
; Syntax ...: $oObject.Charcoal($image, $dest, $factor)
; Param(s) .: $image: path to the image of which to charcolate
;			  $dest : path to the save the image to after it has been altered
;			  $factor:
; Success ..: An image that appears to have been drawn with charcoal.
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_Charcoal($This, Const $image, Const $dest, Const $factor)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-charcoal", $factor, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Contrast
; Syntax ...: $oObject.Contrast($image, $dest, $contrast)
; Param(s) .: $image   : path to the image of which to change the orientation
;			  $dest    : path to the save the image to after it has been altered
;			  $contrast: amount by which to adjust the contrast (-100 | 100)
; Success ..: 1 & An image whose contrast has been adjusted.
; Fail .....: 0 & @error: 1 Invalid image path provided.
;						  2 contrast is out of bounds (-100 | 100)
; Desc. ....:
Func IM_Contrast($This, Const $image, Const $dest, Const $contrast)
	Switch ($contrast < -100) Or ($contrast > 100)
		Case 0
			Switch FileExists($image)
				Case 1
					$this.IMObj.Convert($image, "-brightness-contrast", 'x' & $contrast, $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 1
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Crop
; Syntax ...: $oObject.Crop($image, $dest)
; Param(s) .: $image   : path to the image
; 			  $dest    : path to the save the image to after it has been altered
;			  $geometry: "size of the image that remains after cropping"
; Success ..:
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....: Cut out one or more rectangular regions of the image.            +repage
Func IM_Crop($This, Const $image, Const $dest, Const $geometry)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "+repage", "-crop", $geometry, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name ...: IM_Decipher
; Syntax .: $oObject.Decipher($image, $dest, $pass)
; Param(s): $image: path to the image
; 			$dest: path to save the image to after it has been altered
; 			$pass: path to the '.txt' file containing the passphrase
; Success : 1
; Fail ...: 0 & @error: 1: Invalid image path provided.
;					    2: path to the passphrase file is invalid
;					    3: invalid image format
; Desc. ..: "Decipher and restore pixels that were previously transformed by -encipher. Get the passphrase from the file specified by filename."
Func IM_Decipher($This, Const $image, Const $dest, Const $pass)
	Local $szDrive, $szDir, $szFName, $szExt

	_PathSplit($image, $szDrive, $szDir, $szFName, $szExt)

	Switch FileExists($image)
		Case 1
			Switch FileExists($pass)
				Case 1
					Switch $szExt
						Case ".png"
							$this.IMObj.Convert($image, "-decipher", $pass, $dest)
							Return $this.Success
						Case Else
							Return SetError(3, 0, 0)
					EndSwitch
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Despeckle
; Syntax ...: $oObject.Despeckle($image, $dest)
; Param(s) .: $image: path to the image
; 			  $dest: path to save the image to after it has been altered
; Success ..: 1
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....: "Reduces the speckles in an image."
Func IM_Despeckle($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-despeckle", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name   : IM_Edge
; Syntax : $oObject.Edge($image, $radius, $dest)
; Param(s): $image: path to the image
; 			$dest : path to save the image to after it has been altered
;           $radius: Start with zero.
; Success: 1
; Fail   : 0 & @error: 1: Invalid image path provided.
;                      2: $radius is not a number.
; Desc.  : "Detect edges within an image."
Func IM_Edge($This, Const $image, Const $radius, Const $dest)
	Switch FileExists($image)
		Case 1
			Switch IsNumber($radius)
				Case 1
					$this.IMObj.Convert($image, "-edge", $radius, $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Emboss
; Syntax ...: $oObject.Emboss($image, $radius, $dest)
; Param(s) .: $image: path to the image
; 			  $dest : path to save the image to after it has been altered
;             $radius: Start with zero.
; Success ..: 1
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....: "Emboss an image."
Func IM_Emboss($This, Const $image, Const $radius, Const $dest)
	Switch FileExists($image)
		Case 1
			Switch IsNumber($radius)
				Case 1
					$this.IMObj.Convert($image, "-emboss", $radius, $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name   : IM_Encipher
; Syntax : $oObject.Encipher($image, $dest, $pass)
; Param(s): $image: path to the image: no JPGs or GIFs
; 			$dest: path to save the image to after it has been altered
; 			$pass: path to the '.txt' file containing the passphrase
; Success: 1
; Fail   : 0 & @error: 1: Invalid image path provided.
;					   2: path to the passphrase file is invalid
;					   3: invalid image format
; Desc.  : "Encipher pixels for later deciphering by -decipher. Get the passphrase from the file specified by filename."
Func IM_Encipher($This, Const $image, Const $dest, Const $pass)
	Local $szDrive, $szDir, $szFName, $szExt

	_PathSplit($image, $szDrive, $szDir, $szFName, $szExt)

	Switch FileExists($image)
		Case 1
			Switch FileExists($pass)
				Case 1

					Switch $szExt
						Case ".png"
							$this.IMObj.Convert($image, "-encipher", $pass, $dest)
							Return $this.Success
						Case Else
							Return SetError(3, 0, 0)
					EndSwitch
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Flip
; Syntax ...: $oObject.Flip($image, $dest)
; Param(s) .: $image: path to the image
; 			  $dest: path to save the image to after it has been altered
; Success ..: 1
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....: "Create a mirror image."
Func IM_Flip($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-flip", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Flop
; Syntax ...: $oObject.Flop($image, $dest)
; Param(s) .: $image: path to the image
; 			  $dest: path to save the image to after it has been altered
; Success ..: 1
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....: "Create a mirror image."
Func IM_Flop($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-flop", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Gamma
; Syntax ...: $oObject.Gamma($image, $value, $dest)
; Param(s) .: $image: path to the image
; 			  $dest: path to save the image to after it has been altered
;             $value:
; Success ..:
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_Gamma($This, Const $image, Const $value, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-gamma", $value, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

;= Method ================================================================================
; Name .....: IM _Implode
; Syntax ...: $oObject.Implode($image, $dest, $factor)
; Param(s) .: $image: path to the image
; 			  $dest : path to the save the image to after it has been altered
;			  $factor:
; Success ..:
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....:
Func IM_Implode($This, Const $image, Const $dest, Const $factor)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-implode", $factor, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Modulate
; Syntax ...: $oObject.Modulate($image, $dest, $modulate)
; Param(s) .: $image     : path to the image
; 			  $dest      : path to the save the image to after it has been modulated
;			  $brightness: [optional] amount by which to change the brightness: defaults to 100
;			  $saturation: [optional] amount by which to change the saturation: defaults to 100
;			  $hue       : [optional] amount by which to change the hue: defaults to 100
;			  $color_space: [optional] specify a colorspace
; Success ..:
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Modulate($This, Const $image, Const $dest, Const $brightness = '', Const $saturation = '', Const $hue = '', Const $color_space = "HSL")
	#forceref $color_space
	Switch FileExists($image)
		Case 1
			Local $tmp_brightness = $brightness
			Local $tmp_saturation = $saturation
			Local $tmp_hue        = $hue

			If $brightness = '' Then $tmp_brightness = 100
			If $saturation = '' Then $tmp_saturation = 100
			If $hue        = '' Then $tmp_hue = 100

			Local Const $modulate = String($tmp_brightness & $this.Seperator & $tmp_saturation & $this.Seperator & $tmp_hue)

			$this.IMObj.Convert($image, "-modulate", $modulate, $dest) ; "-set", "option:modulate:colorspace", $color_space,

			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Monochrome
; Syntax ...: $oObject.Monochrome($image, $dest)
; Param(s) .: $image: The directory of the image to make monochrome.
;			  $dest: Final directory of the altered image.
; Success ..: A monochromatic version of the provided image.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Monochrome($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-monochrome", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Negate
; Syntax ...: $oObject.Negate($image, $dest)
; Param(s) .: $image: The directory of the image of which you wish to negate.
;			  $dest: Final directory of the altered image.
; Success ..: Negates the image and writes the image to $dest.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Negate($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-negate", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Normalize
; Syntax ...: $oObject.Normalize($image, $dest)
; Param(s) .: $image: The directory of the image of which you wish to normalize.
;			  $dest: Final directory of the altered image.
; Success ..: Normalizes the image and writes the image to $dest.
; Fail .....: 0 & @error: 1: Invalid image path provided.
Func IM_Normalize($This, Const $image, Const $dest)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-normalize", $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Paint
; Syntax ...: $oObject.Normalize($image, $dest)
; Param(s) .: $image: The directory of the image of which you wish to 'paint'.
;			  $dest: Final directory of the altered image.
; Success ..: "Paints" the image and writes the image to $dest.
; Fail .....: 0 & @error: 1: Invalid image path provided.
; Desc. ....: Makes your image look as though it were an oil painting, kinda.
Func IM_Paint($This, Const $image, Const $dest, Const $radius)
	Switch FileExists($image)
		Case 1
			$this.IMObj.Convert($image, "-paint", $radius, $dest)
			Return $this.Success
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: IM_Polaroid
; Syntax ...: $oObject.Polaroid($image, $dest, $angle)
; Param(s) .: $image: path to the image
; 			  $dest: path to save the image to after it has been altered
;             $angle
; Success ..: 1 &
; Fail .....: 0 & @error: 1:
; Desc. ....: "Simulate a Polaroid picture."
Func IM_Polaroid($This, Const $image, Const $dest, Const $angle = '')
	Switch FileExists($image)
		Case 1
			Switch IsNumber($angle)
				Case 1
					$this.IMObj.Convert($image, "-polaroid", $angle, $dest)
					Return $this.Success
				Case 0
					If $angle = '' Then
						$this.IMObj.Convert($image, "+polaroid", $dest)
					Return $this.Success
					Else
						Return SetError(2, 0, 0)
					EndIf
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Resize
; Syntax ...: $oObject.Resize($image, $dest, $resize)
; Param(s) .: $image : The directory of an image to be resized.
;			  $resize: Desired size.
;			  $dest  : Directory in which to store the resized image.
; Success ..: A resized image snug and secure in its new home with a pretty young wife.
; Fail .....: 0 & @error: 1: $image directory does not exist: not as far as his nagging wife is concerned.
;				  	      2: $resize is not a number: at least not until it clocks back into work on Monday morning.
Func IM_Resize($This, Const $image, Const $dest, Const $resize)
	Switch FileExists($image)
		Case 1
			Switch IsInt($resize)
				Case 1
					$this.IMObj.Convert($image, "-resize", $resize, $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Rotate
; Syntax ...: $oObject.Rotate($image, $degrees, $dest)
; Param(s) .: $image : The directory of an image to be rotated.
;			  $dest  : Directory in which to store the resized image.
;			  $degrees: Desired rotation in degrees (90, -90, 180, -180).
; Success ..: A rotated image located in the specified directory.
; Fail .....: 0 & @error: 1: Invalid image path provided.
;				  	      2: $degrees is not a number.
Func IM_Rotate($This, Const $image, Const $dest, Const $degrees)
	Switch FileExists($image)
		Case 1
			Switch IsInt($degrees)
				Case 1
					$this.IMObj.Convert($image, "-rotate", $degrees, $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Sepiatone
; Syntax ...: $oObject.Sepiatone($image, $degrees, $dest, $threshold)
; Param(s) .: $image : The directory of an image to be rotated.
;			  $dest  : Directory in which to store the resized image.
;			  $degrees: Desired rotation in degrees (90, -90, 180, -180).
; Success ..: A rotated image located in the specified directory.
; Fail .....: 0 & @error: 1: Invalid image path provided.
;				  	      2: $threshold is not a number.
Func IM_Sepiatone($This, Const $image, Const $dest, Const $threshold)
	Switch FileExists($image)
		Case 1
			Switch IsNumber($threshold)
				Case 1
					Local $_threshold = $threshold

					If $_threshold < 0 Then
						$_threshold = 0
					ElseIf $_threshold > 99.9 Then
						$_threshold = 99.9
					EndIf

					$this.IMObj.Convert($image, "-sepia-tone", $_threshold & '%', $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method ================================================================================
;	Name .....: IM_Spread
;	Syntax ...: $oObject.Spread($image, $dest, $amount)
;	Param(s) .: $image: path to the image
;				$dest : path to the save the image to after it has been altered
;				$amount: "... defines the size of the neighborhood around each pixel from which to choose a candidate pixel to swap."
;	Success ..: An image whose pixels have been spread around a bit.
;	Fail .....: 0 & @error: 1: Invalid image path provided.
;						    2: $amount is invalid.
; Desc. ....: "displace image pixels by a random amount."
Func IM_Spread($This, Const $image, Const $dest, Const $amount)
	Switch FileExists($image)
		Case 1
			Switch IsInt($amount)
				Case 1
					$this.IMObj.Convert($image, "-spread", $amount, $dest)
					Return $this.Success
				Case 0
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: IM_Thumbnail
; Syntax ...: $oObject.Thumbnail($image, $dest, $resize)
; Param(s) .: $image: The directory of an image to be resized.
;			  $resize: Desired size.
;			  $dest : Directory in which to store the resized image.
; Success ..: A resized image snug and secure in its new home with a pretty young wife.
; Fail .....: 0 & @error: 1: $image directory does not exist: not as far as his nagging wife is concerned.
;				  	      2: $resize is not a number: at least not until it clocks back into work on Monday morning.
; Desc .....: "This is similar to -resize, except it is optimized for speed and any image profile, other than a color
;				profile, is removed to reduce the thumbnail size."
Func IM_Thumbnail($This, Const $image, Const $dest, Const $resize)
	Switch FileExists($image)
		Case 1
			Switch IsNumber($resize)
				Case 1
					MsgBox(0, '', 1)
					$this.IMObj.Convert($image, "-strip", "-thumbnail", $resize & '>', $dest)
					Return $this.Success
				Case 0
					MsgBox(0, '', 2)
					Return SetError(2, 0, 0)
			EndSwitch
		Case 0
			MsgBox(0, '', 3)
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc
#EndRegion ;**** Convert Methods ****

; = Method =================================================================================
; Name ...: _IM_CopyImagesToTmpDir
; Syntax .: $oObject.CopyImagesToTmpDir($image_list, $tmp_dir)
; Param(s): $image_list:
;			$tmp_dir:
; Success : 1
; Fail ...: 0 @error   : 1: Image(s) were not copied.
;			  @Extended: Ex: 2/4 = "two out of four images failed to copy"
Func _IM_CopyImagesToTmpDir(Const $this, Const $image_list)
	Local $images = $this.ConvertToCSS($image_list)

	Local $image_array = StringSplit($images, $this.Seperator, 2)

	Local Const $up_bound = UBound($image_array) - 1

	Local $flag_array[$up_bound + 1]

	For $i = 0 To $up_bound
		If FileCopy($image_array[$i], $this.tmpDir, 8 + 1) Then
			$flag_array[$i] = 0
		Else
			$flag_array[$i] = 1
		EndIf
	Next

	Local $error_count = 0

	For $i = 0 To $up_bound
		If $flag_array[$i] = 1 Then $error_count += 1
	Next

	Local $return

	If $error_count = 0 Then
		$return = 0
	Else
		$return = SetError(1, $error_count & '/' & $up_bound, 0)
	EndIf

	Return $return
EndFunc

;= Method =================================================================================
; Name .....: _IM_StartUp
; Syntax ...: $oObject.StartUp([$error_handler])
; Param(s)..: $error_handler: Set to true to register the object with the COM Error handler false otherwise.
; Success ..: An object named "IMagickImage".
; Fail .....: @error: 1 -- ImageMagick object was not created.
; Modified .: trancexx -- thanks for _AutoItObject_ObjCreateEx & for your work on AutoItObject!
Func IM_StartUp($This, Const $error_handler = True)
	Local Const $IMObj = _AutoItObject_ObjCreateEx($This.DLL, "{5630BE5A-3F5F-4BCA-A511-AD6A6386CAC1}")

	Switch IsObj($IMObj)
		Case 1
			If $error_handler Then $oMyError = ObjEvent("AutoIt.Error", "IM_MyErrFunc")
			$this.IMOBj = $IMObj
			Return $this.IMObj
		Case 0
			MsgBox(0, '', "$IMObj is not an object.")
			SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: _IM_ShutDown
; Syntax ...: $oObject.ShutDown()
Func _IM_ShutDown($This)
	$this.IMObj = ''
	$oMyError = ''
	Return $this.IMObj
EndFunc

; = Method =================================================================================
; Name ...: _IM_ColorFormatter
; Syntax .: $oObject.ColorFormatter($color)
; Param(s): $color: Formats the $color method argument.
; Success :
; Fail ...:
Func _IM_ColorFormatter(Const $This, Const $color)
	Local $tmp_color

	Select
		Case IsInt($color)
			$tmp_color = '#' & Hex($color, 6)
		Case IsString($color)
			If $color = '' Then Return "#FFFFFF"
			$tmp_color = $this.ColorVerifier($color)
		Case Else
			$tmp_color = "#FFFFFF"
	EndSelect

	Return $tmp_color
EndFunc

; = Method ================================================================================
; Name ...: _IM_ColorVerifier
; Syntax .: $oObject.ColorVerifier($color)
; Param(s): $color: An IM supported color name
; Success : 1 An IM supported color name stripped of all whitespace.
; Fail ...: 0 & @error: 2: Color not supported by IM
; Desc. ..: Checks a color found at $color against a list of ImageMagick recognized list of colors.
Func _IM_ColorVerifier(Const $This, Const $color)
	#forceref $this
	Local Const $file_name = @ScriptDir & "\List of IM Colors.txt"
	Local $im_colors

	Switch _FileReadToArray($file_name, $im_colors)
		Case 1
;~ 			For $i = 0 To UBound($im_colors) ConsoleWrite($im_colors[$i] & @LF)

			Local Const $color_amount = $im_colors[0] - 1

			_ArrayDelete($im_colors, 0)

			_ArraySort($im_colors)

			For $i = 0 To $color_amount
				$im_colors[$i] = StringLower(StringStripWS($im_colors[$i], 8))
			Next

			Local Const $search_color = StringLower(StringStripWS($color, 8))

			Switch _ArrayBinarySearch($im_colors, $search_color)
				Case -1
					Return SetError(2, 0, 0)
				Case Else
					Return $search_color
			EndSwitch
		Case 0
			Return SetError(1, 0, 0)
	EndSwitch
EndFunc

; = Method =================================================================================
; Name .....: _IM_ConvertToCSS
; Syntax ...: $oObject.ConvertToCSS($image_list)
; Param(s) .: $image_list: An array, directory, or a comma separated string containing the images to be appended.
; Success ..: A string of comma separated images.
; Fail .....: 0 & @error: 1: Invalid parameter, $image_list is not a string.
;					      2: Invalid parameter, $image_list is not comma separated
; Desc. ....: This method will return a string of images seperated by commas when given an array of images.
Func _IM_ConvertToCSS($This, Const ByRef $image_list)
	Switch IsArray($image_list)
		Case 0 ; $image_list is not an array, check to see if it is a directory
			Switch FileExists($image_list)
				Case 0 ; is not a valid directory, check to see if it is a string
					Switch IsString($image_list)
						Case 0
							Return SetError(1, 0, 0)
						Case 1 ; is a string, now check to see if it is comma seperated
							Switch StringInStr($image_list, $this.Seperator)
								Case 0
									Return SetError(2, 0, 0)
								Case Else ; nothing needs doing
									Return $image_list
							EndSwitch
					EndSwitch
				Case 1 ; nothing needs doing
					Return $image_list
			EndSwitch
		Case 1 ; $image_list is an array, convert to comma separated string
			Return _ArrayToString($image_list, $this.Seperator)
	EndSwitch
EndFunc

; = Method ================================================================================
; Name .....: _IM_GravityVerifier
; Syntax ...: $oObject.GravityVerifier($gravity)
; Param(s) .: $gravity: Checks $gravity against a list of known ImageMagick supported gravities.
; Success ..: A string of comma separated images.
; Fail .....: 0 & @error: 1: Invalid parameter, $gravity is not a string.
;					      2: Unsupported gravity parameter.
; Desc. ....:
Func _IM_GravityVerifier(Const $this, Const $gravity)
	If Not IsString($gravity) Then Return SetError(1, 0, 0)

	If $gravity = '' Or $gravity = ' ' Then Return "NorthWest" ; default

	Local Const $gravity_list = $this.GravityList

	Local Const $result = _ArraySearch($gravity_list, $gravity)

	Switch $result
		Case -1
			Return SetError(2, 0, 0)
		Case Else
			Switch StringLower($gravity)
				Case 'nw'
					Return "NorthWest"
				Case 'n'
					Return "North"
				Case "ne"
					Return "NorthEast"
				Case 'w'
					Return "West"
				Case 'c'
					Return "Center"
				Case 'e'
					Return "East"
				Case "sw"
					Return "SouthWest"
				Case 's'
					Return "South"
				Case "se"
					Return "SouthEast"
				Case Else
					Return $gravity_list[$result]
			EndSwitch
	EndSwitch
EndFunc
